package com.niit.service.impl;

import com.niit.entity.User;
import com.niit.repository.UserRepository;
import com.niit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public User findByInfoIdAndUserType(Integer id, Boolean type) {
        return userRepository.findUserByInfo_IdAndUserType(id, type);
    }

    @Override
    public User save(User user) {
        return userRepository.save(user);
    }

    @Override
    public Page<User> findAll(String phone, String username, String realName, String gender, Boolean userType, Pageable pageable) {
        Specification<User> spec = UserRepository.UserSpecifications.withDynamicQuery(phone, username, realName, gender, userType);
        return userRepository.findAll(spec, pageable);
    }

    @Override
    public User findById(Integer id) {
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public void deleteById(Integer id) {
        userRepository.deleteById(id);
    }
}
